package org.aceor.mddal.group;

import org.aceor.mddal.client.jdbc.DataSourceFacade;
import org.aceor.mddal.group.selector.DBSelector;
import org.aceor.mddal.group.util.RuntimeSelectorHolder;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

/**
 * Created by lxue on 16/4/8.
 */
public class Main {

    private ApplicationContext applicationContext;

    @Before
    public void init() {
        applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
    }
    @Test
    public void test_group_build() {
        DataSourceFacade dataSourceFacade = applicationContext.getBean("myDataSource", DataSourceFacade.class);

        assertEquals(2, dataSourceFacade.getDsMapHolder().get().size());

        GroupDataSource dataSource = (GroupDataSource) dataSourceFacade.getDsMapHolder().get().get("MDDAL_APP_0_GROUP");
        assertEquals(2,dataSource.getDataSourceWrapperMap().size());
    }

    @Test
    public void test_group_selector() {
        DataSourceFacade dataSourceFacade = applicationContext.getBean("myDataSource", DataSourceFacade.class);

        assertEquals(2, dataSourceFacade.getDsMapHolder().get().size());

        GroupDataSource dataSource = (GroupDataSource) dataSourceFacade.getDsMapHolder().get().get("MDDAL_APP_0_GROUP");

        RuntimeSelectorHolder runtimeSelectorHolder = dataSource.getRuntimeSelectorHolder();

        assertNotNull(runtimeSelectorHolder);

        assertNotNull(runtimeSelectorHolder.getDBSelector(true));
        assertNotNull(runtimeSelectorHolder.getDBSelector(false));
        /**
         * <group id="MDDAL_APP_0_GROUP">
         <atom ref="MDDAL_APP_0_ATOM" dbStatus="RW" weight="r:10,w:10,p:1,q:0"/>
         <atom ref="MDDAL_APP_1_ATOM" dbStatus="R" weight="r:0,w:0,p:2,q:0"/>
         </group>
         */
        DBSelector rDBSelector = runtimeSelectorHolder.getDBSelector(true);
        assertNotNull(rDBSelector.select());
        for (int i=0; i<10; i++) {
            assertEquals("MDDAL_APP_0_ATOM",((DataSourceWrapper)rDBSelector.select()).getDataSourceKey());
        }

        DBSelector wDBSelector = runtimeSelectorHolder.getDBSelector(false);
        assertNotNull(wDBSelector.select());
        for (int i=0; i<10; i++) {
            assertEquals("MDDAL_APP_0_ATOM",((DataSourceWrapper)wDBSelector.select()).getDataSourceKey());
        }
    }

    @Test
    public void test_group_selector_getds() {
        DataSourceFacade dataSourceFacade = applicationContext.getBean("myDataSource", DataSourceFacade.class);

        assertEquals(2, dataSourceFacade.getDsMapHolder().get().size());

        GroupDataSource dataSource = (GroupDataSource) dataSourceFacade.getDsMapHolder().get().get("MDDAL_APP_0_GROUP");

        RuntimeSelectorHolder runtimeSelectorHolder = dataSource.getRuntimeSelectorHolder();

        assertNotNull(runtimeSelectorHolder);

        assertNotNull(runtimeSelectorHolder.getDBSelector(true));
        assertNotNull(runtimeSelectorHolder.getDBSelector(false));
        /**
         * <group id="MDDAL_APP_0_GROUP">
         <atom ref="MDDAL_APP_0_ATOM" dbStatus="RW" weight="r:10,w:10,p:1,q:0"/>
         <atom ref="MDDAL_APP_1_ATOM" dbStatus="R" weight="r:0,w:0,p:2,q:0"/>
         </group>
         */
        //如果权重为0,就返回ds=null
        DBSelector rDBSelector = runtimeSelectorHolder.getDBSelector(true);
        assertNull(rDBSelector.get("MDDAL_APP_1_ATOM"));
    }
}
